// 父组件index.vue
<template>
    <div class="waterfall-list-container" ref="containerRef">
        <waterfall :lazyload="false" backgroundColor="transparent" :breakpoints="breakpoints" :gutter="8" :list="list">
            <template #item="{ item }">
                <div class="card-content">
                    <LazyImg class="img" :url="item.imageUrl" />
                    <div class="prompt">
                        <p>{{ item.question }}</p>
                        <div class="action">
                            <div @click="drawing">画同款</div>
                            <div @click="copy(item)">复制</div>
                        </div>
                    </div>
                </div>
            </template>
        </waterfall>
        <div class="loading">
            <a-spin :spinning="loading" :indicator="indicator" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, reactive, ref } from "vue";
import { drawingPage, DrawingPageQuestionData } from "@/api/draw";

import { LazyImg, Waterfall } from "vue-waterfall-plugin-next";
import "vue-waterfall-plugin-next/dist/style.css";
import { LoadingOutlined } from "@ant-design/icons-vue";
import { h } from "vue";
import debounce from "lodash/debounce";
import useClipboard from "vue-clipboard3";
import { message } from "ant-design-vue";

const { toClipboard } = useClipboard();

const indicator = h(LoadingOutlined, {
    style: {
        fontSize: "24px",
    },
    spin: true,
});

const list = ref<any>([]);
const breakpoints = ref({
    1400: {
        //当屏幕宽度小于等于1200
        rowPerView: 5,
    },
    1200: {
        //当屏幕宽度小于等于1200
        rowPerView: 4,
    },
    800: {
        //当屏幕宽度小于等于800
        rowPerView: 3,
    },
    500: {
        //当屏幕宽度小于等于500
        rowPerView: 2,
    },
});
const containerRef = ref();
const loading = ref(false);
const queryParams = reactive({
    pageNo: 1,
    pageSize: 40,
    question: "",
    categoryCode: "",
});
const total = ref(0);
const getList = debounce(async () => {
    loading.value = true;
    const { data } = await drawingPage(queryParams);
    list.value = [...list.value, ...data.robotQuestions];
    total.value = data.total;
    setTimeout(() => {
        loading.value = false;
    }, 1000);
}, 600);

const drawing = () => {};

const copy = async (item: DrawingPageQuestionData) => {
    try {
        // 复制
        await toClipboard(item.question);
        message.success("复制成功");
        // 复制成功
    } catch (e) {
        console.log(e);
        // 复制失败
    }
};

onMounted(() => {
    getList();
    containerRef.value.addEventListener("scroll", (res: any) => {
        const height = res.target;
        const clientHeight = height.clientHeight;
        const scrollTop = height.scrollTop;
        const scrollHeight = height.scrollHeight;
        const totalScroll = parseInt(clientHeight + scrollTop);
        if (totalScroll + 1 >= scrollHeight) {
            if (list.value.length >= total.value) return;
            queryParams.pageNo++;
            if (!loading.value) {
                getList();
            }
        }
    });
});
onUnmounted(() => {
    containerRef.value?.removeEventListener("scroll");
});
</script>

<style lang="less" scoped>
.waterfall-list-container {
    height: 100%;
    overflow: auto;
    .card-content {
        border-radius: 4px;
        box-sizing: border-box;
        overflow: hidden;
        position: relative;

        &:hover {
            .prompt {
                transform: translateY(0);
            }
        }

        .img {
            height: auto;
            width: 100%;
            transition: all 0.2s ease-in-out;
            transform: scale(1);
            cursor: pointer;
            &:hover {
                transform: scale(1.1);
            }
            img {
                width: 100%;
            }
        }
        .prompt {
            width: 100%;
            color: #fff;
            background-color: #9494948c;
            position: absolute;
            bottom: 0;
            transform: translateY(100%);
            transition: all 0.25s ease-in-out;
            padding-top: 3px;
            padding-bottom: 3px;
            padding-left: 10px;
            padding-right: 10px;
            p {
                line-height: 110%;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis;
            }
            .action {
                display: flex;
                justify-content: flex-end;
                align-items: center;
                grid-gap: 10px;
                margin-top: 10px;
                > div {
                    cursor: pointer;
                    padding: 3px 6px;
                    border: 1px solid;
                    border-radius: 6px;
                    transition: all 0.2s ease-in-out;
                    font-size: 12px;
                }
            }
        }
    }
    .loading {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 20px;
    }
}
</style>
